package main

import (
	"fmt"
	"github.com/schollz/progressbar/v3"
	"io"
	"net/http"
	"os"
	"path/filepath"
	"sync"
	"time"
)

func downloadImage(url string, filename string, sem chan struct{}, wg *sync.WaitGroup, bar *progressbar.ProgressBar) {
	defer wg.Done()
	sem <- struct{}{}
	defer func() { <-sem }()

	resp, err := http.Get(url)
	if err != nil {
		fmt.Println("下载失败：", err)
		bar.Add(1)
		return
	}
	defer resp.Body.Close()

	out, err := os.Create(filepath.Join("images", filename))
	if err != nil {
		fmt.Println("文件创建失败：", err)
		bar.Add(1)
		return
	}
	defer out.Close()

	_, err = io.Copy(out, resp.Body)
	if err != nil {
		fmt.Println("保存失败：", err)
		bar.Add(1)
		return
	}

	bar.Add(1)
}

func download() {
	os.MkdirAll("images", os.ModePerm)

	urls := []string{
		"https://picsum.photos/seed/1/600/400",
		"https://picsum.photos/seed/2/600/400",
		"https://picsum.photos/seed/3/600/400",
		"https://picsum.photos/seed/4/600/400",
		"https://picsum.photos/seed/5/600/400",
		"https://picsum.photos/seed/6/600/400",
		"https://picsum.photos/seed/7/600/400",
		"https://picsum.photos/seed/8/600/400",
		"https://picsum.photos/seed/9/600/400",
		"https://picsum.photos/seed/10/600/400",
		"https://picsum.photos/seed/11/600/400",
		"https://picsum.photos/seed/12/600/400",
		"https://picsum.photos/seed/13/600/400",
		"https://picsum.photos/seed/14/600/400",
		"https://picsum.photos/seed/15/600/400",
		"https://picsum.photos/seed/16/600/400",
		"https://picsum.photos/seed/17/600/400",
		"https://picsum.photos/seed/18/600/400",
		"https://picsum.photos/seed/19/600/400",
		"https://picsum.photos/seed/20/600/400",
		"https://picsum.photos/seed/21/600/400",
		"https://picsum.photos/seed/22/600/400",
		"https://picsum.photos/seed/23/600/400",
		"https://picsum.photos/seed/24/600/400",
		"https://picsum.photos/seed/25/600/400",
		"https://picsum.photos/seed/26/600/400",
		"https://picsum.photos/seed/27/600/400",
		"https://picsum.photos/seed/28/600/400",
		"https://picsum.photos/seed/29/600/400",
		"https://picsum.photos/seed/30/600/400",
		"https://picsum.photos/seed/31/600/400",
		"https://picsum.photos/seed/32/600/400",
		"https://picsum.photos/seed/33/600/400",
		"https://picsum.photos/seed/34/600/400",
		"https://picsum.photos/seed/35/600/400",
		"https://picsum.photos/seed/36/600/400",
		"https://picsum.photos/seed/37/600/400",
		"https://picsum.photos/seed/38/600/400",
		"https://picsum.photos/seed/39/600/400",
		"https://picsum.photos/seed/40/600/400",
		"https://picsum.photos/seed/41/600/400",
		"https://picsum.photos/seed/42/600/400",
		"https://picsum.photos/seed/43/600/400",
		"https://picsum.photos/seed/44/600/400",
		"https://picsum.photos/seed/45/600/400",
		"https://picsum.photos/seed/46/600/400",
		"https://picsum.photos/seed/47/600/400",
		"https://picsum.photos/seed/48/600/400",
		"https://picsum.photos/seed/49/600/400",
		"https://picsum.photos/seed/50/600/400",
	}

	start := time.Now()
	var wg sync.WaitGroup
	sem := make(chan struct{}, 1)

	// 初始化进度条
	bar := progressbar.NewOptions(len(urls),
		progressbar.OptionSetDescription("📦 下载中"),
		progressbar.OptionShowCount(),
		progressbar.OptionSetWidth(30),
		progressbar.OptionClearOnFinish(),
		progressbar.OptionSpinnerType(14),
	)

	for i, url := range urls {
		wg.Add(1)
		filename := fmt.Sprintf("image-%02d.jpg", i+1)
		go downloadImage(url, filename, sem, &wg, bar)
	}

	wg.Wait()
	fmt.Println("\n🎉 所有图片下载完成，总耗时：", time.Since(start))
}

func test(i int, sem chan struct{}, wg *sync.WaitGroup) {
	defer wg.Done()
	sem <- struct{}{}
	defer func() { <-sem }()
	println("并发并发 -> ", i)
}
func main() {
	// download()
	var wg sync.WaitGroup
	sem := make(chan struct{}, 2)
	for i := range 100 {
		wg.Add(1)
		go test(i, sem, &wg)
	}

	wg.Wait()
}
